package com.mytest.baodianofsunflower;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.widget.Toast;

import com.mytest.util.Globals;
import com.mytest.util.QuestionDao;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IndexActivity extends AppCompatActivity {
    private  List<Map<String,Object>> temvalues ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        Globals.init(this);
        getNewsInfo();
    }
    private void getNewsInfo() {
        Thread t = new Thread(){
            @Override
            public void run() {
                String path = "http://192.168.0.109:8080/news.xml";
                try {
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(8000);
                    conn.setReadTimeout(8000);
                    Log.i("hahaha","判断连接");
                    if(conn.getResponseCode() == 200){
                        //流里的信息是一个xml文件的文本信息，用xml解析器去解析，而不要作为文本去解析
                        InputStream is = conn.getInputStream();
                        Log.i("hahah","qwerqwer"+is);
                        temvalues = getNewsFromStream(is);
                        Globals.INCOUNT = temvalues.size();
                        Log.i("hahah", "$$$$$$$$$$$$$$$$$$$$$$$$" + temvalues.size());
                        Log.i("hahah", "1234-------12" + temvalues.get(0).get("question"));
                        Thread t = new Thread(){
                            @Override
                            public void run() {
                                Log.i("hahah", "$$$$$$$$123$$$$$$$$$$$$$$$$" + temvalues.size()+" "+QuestionDao.getCount(""));
                                if(temvalues.size()== QuestionDao.getCount("")&&temvalues.size()!=0){
                                    for(int i=0;i<temvalues.size();i++){
                                        Boolean isSame = temvalues.get(i).get("question").
                                                equals(QuestionDao.loadData().get(i).get("question"))&&
                                                temvalues.get(i).get("answer").
                                                        equals(QuestionDao.loadData().get(i).get("answer"));
                                        if(!isSame){
                                            QuestionDao.deleteAllData();
                                            for(int x=0;x<temvalues.size();x++){
                                                Log.i("hahah","$$$---"+temvalues.get(x).get("question")+"----$$$");
                                                QuestionDao.insertData(temvalues.get(x));
                                            }
                                            break;
                                        }else if(i==temvalues.size()-1){
                                            try {
                                                sleep(2000);
                                                Log.i("hahah","$$$$$$$$$$$$$$$$$$$$$$$$");
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }

                                }else {
                                    QuestionDao.deleteAllData();
                                    for(int i=0;i<temvalues.size();i++){
                                        Log.i("hahah","$$$---"+temvalues.get(i).get("question")+"----$$$");
                                        QuestionDao.insertData(temvalues.get(i));
                                    }
                                }
                                Intent intent = new Intent(IndexActivity.this,MainActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        };
                        t.start();
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        t.start();
    }
    private List<Map<String ,Object>> getNewsFromStream(InputStream is) {
        //从XML对象中获取XML文件解析器对象。
        List<Map<String ,Object>> values =null;
        Map<String,Object> map = null;
        XmlPullParser xp = Xml.newPullParser();
        try {
            xp.setInput(is, "utf-8");

            //获取事件类型，通过事件类型判断出当前解析的是和什么节点
            int type = xp.getEventType();

            while(type != XmlPullParser.END_DOCUMENT){
                switch (type) {
                    case XmlPullParser.START_TAG:
                        if("newslist".equals(xp.getName())){
                            values = new ArrayList<>();
                        }
                        else if("question".equals(xp.getName())){
                            map= new HashMap<>();
                        }
                        else if("title".equals(xp.getName())){
                            String question = xp.nextText();
                           // Log.i("hahah", "------" + question + "--------");
                            map.put("question",question);
                        }
                        else if("detail".equals(xp.getName())){
                            String answer= xp.nextText();
                           // Log.i("hahah", "------" + answer + "--------");
                            map.put("answer",answer);
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if("question".equals(xp.getName())){
                          //  Log.i("hahah","-----13451---"+map.get("answer"));
                            values.add(map);
                        }
                        break;

                }
                //指针移动到下一个节点并返回事件类型
                type = xp.next();
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.i("hahah","fqhweuoifhhf--"+values.get(0).get("question"));
        return values;
    }
}
